const data = [
  {
    id: 0,
    parentId: null,
    text: '第一级文字'
  }, {
    id: 1,
    parentId: 0,
    text: '第二级文字'
  }, {
    id: 2,
    parentId: 0,
    text: '第三级文字'
  }, {
    id: 3,
    parentId: null,
    text: '第四级文字'
  }, {
    id: 4,
    parentId: null,
    text: '第五级文字'
  }, {
    id: 5,
    parentId: 3,
    text: '第六级文字'
  }, {
    id: 6,
    parentId: 3,
    text: '第七级文字'
  }
];

// 方案1
function toTree(data) {
  let arr = data.filter(item => item.parentId === null);
  arr.forEach(item => {
    let children = data.filter(cur => cur.parentId === item.id);
    item.children = children;
  })
}

// 利用map
function toTree2(data) {
  let map = new Map(), result = [];
  data.forEach(item => {
    map.set(item.id, item)
  });
  data.forEach(item => {
    let { parentId } = item;
    if(parentId === null) {
      result.push(item);
      return
    }
    parent = map.get(parentId);
    parent.children?parent.children.push(item):parent.children = [item];
  });
  return result;
}